package best.driver.android.Utils;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import best.driver.android.R;
import best.driver.android.View.ProgressDialogView;

public class ProgressDialogUtil {

    private Context context;
    private Dialog dialog;
    private ProgressDialogView dialogView;

    public ProgressDialogUtil(Context context) {
        this.context = context;
        dialogView = new ProgressDialogView(context);
        dialog = new Dialog(context, R.style.loading_dialog);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setContentView(dialogView);
    }

    public void show() {
        show(null);
    }

    public void show(String text) {
        show(text, true);
    }

    public void show(String text, final boolean cancelable) {
        if (TextUtils.isEmpty(text)) {
            text = context.getResources().getString(R.string.default_waiting);
        }
        dialog.setCancelable(cancelable);
        dialogView.setText(text);
        if (dialog.isShowing()) {
            return;
        }
        dialog.show();
    }

    public void hide() {

        try {
            dialog.dismiss();
            dialog.setCancelable(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
